package model.persistence.dao;
import model.persistence.vo.Bairro;
import model.persistence.vo.Cinema;
import model.persistence.vo.Compra;
import model.persistence.vo.Filme;
import model.persistence.vo.Sessao;
import model.persistence.vo.Sala;
import model.persistence.vo.Usuario;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    
    private static final SessionFactory sessionFactory;
    private static final Configuration config;
    
    
    static {
        try {
            config = new AnnotationConfiguration()
            
            .addAnnotatedClass(Bairro.class)
            .addAnnotatedClass(Compra.class)
            .addAnnotatedClass(Sessao.class)
            .addAnnotatedClass(Sala.class)
            .addAnnotatedClass(Cinema.class)
            .addAnnotatedClass(Usuario.class)
            .addAnnotatedClass(Filme.class)
            
            .setProperty("hibernate.connection.url", "jdbc:mysql://localhost/semfila")
            
            .setProperty("hibernate.connection.username", "semfila")
            .setProperty("hibernate.connection.password", "1234")
            
            .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
            
            .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLMyISAMDialect")
            
            .setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory")
            
            .setProperty("hibernate.current_session_context_class", "thread")
            
            .setProperty("hibernate.show_sql", "true");
            
            sessionFactory = config
                    .buildSessionFactory();
            
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static Session getSession() throws HibernateException {
        return sessionFactory.openSession();
    }
    
    public static Configuration getConfig() {
        return config;
    }
}
